Completed
Push — master ( 3b2975...5480a6 )
by Dimas
15:02
created

aacaller.ts ➔ getFuncName   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
/**
2
 * call_user_func
3
 * @param functionName function name
4
 */
5
function ___call(functionName: string, context?: Window /*, args?: any*/) {
6
  var args = Array.prototype.slice.call(arguments, 2);
7
  var namespaces = functionName.split(".");
8
  var func = namespaces.pop();
9
  if (typeof window[func] != "undefined") {
10
    window[func](args);
11
  } else if (context && context instanceof Window) {
12
    if (typeof context[func] != "undefined") {
13
      context[func](args);
14
    }
15
  } else {
16
    try {
17
      eval(functionName);
18
    } catch (error) {
19
      console.error(error);
20
      console.error(functionName + " is not function");
21
    }
22
  }
23
}
24
25
/**
26
 * call_user_func
27
 * @param functionName
28
 * @param context
29
 * @param args
30
 */
31
function call_user_func(
32
  functionName: string,
33
  context: Window & typeof globalThis,
34
  args: any
35
) {
36
  var args = Array.prototype.slice.call(arguments, 2);
37
  var namespaces = functionName.split(".");
38
  var func = namespaces.pop();
39
  for (var i = 0; i < namespaces.length; i++) {
40
    context = context[namespaces[i]];
41
  }
42
  return context[func].apply(context, args);
43
}
44
45
if (isnode()) {
46
  module.exports.___call = ___call;
47
}
48
49
/**
50
 * Is Node ?
51
 */
52
function isnode() {
53
  if (typeof module !== "undefined" && module.exports) {
54
    return true;
55
  }
56
}
57
58
if (isnode()) {
59
  module.exports.isnode = isnode;
60
}
61
62
/**
63
 * Make function async
64
 * @param callback
65
 */
66
function async_this(callback: Function): Promise<any> {
67
  return new Promise(function (resolve, reject) {
68
    if (typeof callback == "function") {
69
      callback();
70
      resolve(true);
71
    } else {
72
      reject(new Error("callback is not function"));
73
    }
74
  });
75
}
76
77
/**
78
 * call_user_func
79
 * @param func function name
80
 */
81
function __call(func: string) {
82
  this[func].apply(this, Array.prototype.slice.call(arguments, 1));
83
}
84
85
/**
86
 * check empty
87
 * @param str
88
 */
89
function empty(str: string | null | undefined | number | boolean) {
90
  var type = typeof str;
91
  if (type == "string" || type == "number") {
92
    str = str.toString().trim();
93
  }
94
  switch (str) {
95
    case "":
96
    case null:
97
    case false:
98
    case type == "undefined": //typeof (str) == "undefined"
99
      return true;
100
    default:
101
      return false;
102
  }
103
}
104
105
if (isnode()) {
106
  module.exports.empty = empty;
107
}
108
109
/**
110
 * Get current function name
111
 */
112
function getFuncName() {
113
  return getFuncName.caller.name;
114
}
115
116
if (isnode()) {
117
  module.exports.getFuncName = getFuncName;
118
}
119